# 数据导入
import numpy as np
import matplotlib.pyplot as plt
# 定义各种处理函数
# 处理平均数/中位数/众数
def averageAndMedianAndMax(arr):
    mid=np.median(arr)
    return  mid, max
# 将分数段进行切分，且分段看分成几段0-60，60-70,70-80,80-90,90-100,100-110,110-120
def getRatesAndMid(arr):
    data=[]
    data0=[]
    data1=[]
    data2=[]
    data3=[]
    data4=[]
    data5=[]
    data6=[]
    count=len(arr)
    for i in arr:
        if i>=0 and i<80:
            data0.append(i)
        elif i>=60 and i<70:
            data1.append(i)
        elif i>=70 and i<80:
            data2.append(i)
        elif i >= 80 and i < 90:
            data3.append(i)
        elif i >= 90 and i < 100:
            data4.append(i)
        elif i >= 100 and i < 110:
            data5.append(i)
        elif i >= 110 and i <=120:
            data6.append(i)
    data.append(data0)
    data.append(data1)
    data.append(data2)
    data.append(data3)
    data.append(data4)
    data.append(data5)
    data.append(data6)
    rates=[]
    mids=[]
    for i in data:

        if(len(i)!=0):
            rates.append(len(i))
            mids.append(np.median(i))
        # print(averageAndMedianAndMax(i)[0])
    return rates,mids,count
    # print(rates)
    # print(mids)

    # print(data)
# 需要求每个阶段的频率和中位数
def test(rates,mids,count):
    for i in range(len(rates)):
        rates[i]=rates[i]/count
        print(rates[i])
    plt.bar(mids,rates,width=3.5,facecolor="lightskyblue",edgecolor="white",align="center")
    for x,y in zip(mids,rates):
        plt.text(x+0.5,y+0.001,"%.2f"%y ,ha="center",va="bottom")
        # plt.text(x + 0.5, -0.02, "%.2f"%x, ha="center", va="bottom")
    # plt.plot(mids,[0,0,0,0,0],marker="+")
    plt.title("1234")
    plt.xlabel("points")
    plt.ylabel("frequency")
    plt.show()
# 定义绘画函数

# 读取文件
def readFileAndGetGrade():
    data = []
    for line in open("data.txt", "r"):  # 设置文件对象并读取每一行文件
        num = list(map(float, line.split()))
        data.append(num)
    chaineseGrade = []
    for i in data:
        chaineseGrade.append(i[1])
    return chaineseGrade
# test()
rates,mids,count=getRatesAndMid(readFileAndGetGrade())
test(rates,mids,count)
